package com.atiguigu.springcloud.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.atguigu.springcloud.entities.CommonResult;
import com.atguigu.springcloud.entities.Payment;
import com.atiguigu.springcloud.myHandler.MyCustomerBlockHandler;
import com.atiguigu.springcloud.myHandler.CustomerFallback;
import com.atiguigu.springcloud.service.PaymentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

/**
 * @Author : 李红磊
 * @Date : 2021/05/27/12:50
 * @Description :
 */
@RestController
@Slf4j
@RequestMapping("consumer")
public class CircleBreakerController {

    @Value("${service-url.nacos-user-service}")
    public   String SERVICE_UR;


    @Autowired
    private PaymentService paymentServiceImpl;

    @Autowired
    private RestTemplate restTemplate;


    @GetMapping("/paymentSQL/{id}")
    @SentinelResource(value = "fallback",
            fallback = "handlerFallback1",//程序异常的备用
            fallbackClass = CustomerFallback.class,
    blockHandler = "blockHandler1",   //sentinel异常方案
    blockHandlerClass = MyCustomerBlockHandler.class
    )
    public String p1(@PathVariable("id")Long id){
        String result = restTemplate.getForObject(SERVICE_UR + "/paymentSQL/" + id, String.class);

        if(id==4){
            throw new RuntimeException("参数错误奥");
        }else if(result==null){
            throw new RuntimeException("没有数据");
        }
        return result;
    }



    //openFeion负载均衡
    @GetMapping("/pay/{id}")
    CommonResult<Payment> paymentSQL(@PathVariable("id")Long id){
        return paymentServiceImpl.paymentSQL(id);
    }






   /* @GetMapping("/payment")
    @SentinelResource(value = "fallback65",
            blockHandlerClass = MyCustomerBlockHandler.class,
            blockHandler = "blockHandler1"
    )
    public String test2(){

        return "试试呀";
    }




    @GetMapping("/test")
    @SentinelResource(value = "fallback2",
            blockHandlerClass = MyCustomerBlockHandler.class,
            blockHandler = "deal_handler"
            )
    public String myTest(@RequestParam(value = "p1",required = false)String ps,
                         @RequestParam(value = "p2",required = false)String p2){

        return "66666";
    }

    *//*public String deal_handler(String p1,String p2,BlockException exception){
        return "发生错误了!!!!";
    }*//*



    @GetMapping("/testHotKey")
    @SentinelResource(value = "ttt",blockHandler = "deal_testHotKey")
    public String testHotKey(@RequestParam(value = "p1",required = false)String ps,
                             @RequestParam(value = "p2",required = false)String p2){

        return "testHotKey";
    }

    public String deal_testHotKey(String p1, String p2, BlockException exception){

        return "deal_testHotKey,o(╥﹏╥)o";

    }*/


}
